package com.eupmt.lab3.persistence.ibatis.dao;

import com.eupmt.lab3.domain.BoGarantia;
import com.eupmt.lab3.domain.Client;
import com.eupmt.lab3.domain.Producte;
import com.eupmt.lab3.exceptions.BoGarantiaJaExisteixException;
import com.eupmt.lab3.exceptions.BoGarantiaNoExisteixException;
import com.eupmt.lab3.persistence.ibatis.PersistenceManager;
import com.eupmt.lab3.persistence.ibatis.dao.ibator.IbatorBoGarantiaDAOImpl;
import com.eupmt.lab3.utils.Log;
import com.ibatis.sqlmap.client.SqlMapClient;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import static com.eupmt.lab3.utils.Constants.*;

/**
 *
 * @author sony
 */
public class BoGarantiaDAOImpl extends IbatorBoGarantiaDAOImpl implements BoGarantiaDAO {

    private SqlMapClient sqlMapClient;
    private Log log = null;

    public BoGarantiaDAOImpl() {
        super();
        sqlMapClient = PersistenceManager.getInstance().getSqlMapClient();
        log = new Log();
    }

    public void saveBoGarantia(BoGarantia boGarantia) throws Exception {
        log.infoLog("BoGarantia a fer save: " + boGarantia.getIdgarantia());


        if (boGarantia.getIdgarantia() == UNDEFINED) {
            insertBoGarantia(boGarantia);
        } else {
            updateBoGarantia(boGarantia);
        }

    }

    public void insertBoGarantia(BoGarantia boGarantia) throws Exception {

        try{
            sqlMapClient.insert("bogarantia.insert",boGarantia);

        }catch(SQLException sqle){
            if (sqle.getErrorCode()== BOGARANTIA_JA_EXISTEIX){
                throw new BoGarantiaJaExisteixException(sqle.getMessage());
            }
            log.infoLog("Insert SQLErrorCode: "+ sqle.getErrorCode());
        }

    }

    public void updateBoGarantia(BoGarantia boGarantia) throws Exception {
        
        try{
            log.infoLog("update bogarantia");
            sqlMapClient.update("bogarantia.update", boGarantia);
        }catch (SQLException sqle){
            log.infoLog("SQLErrorCode:"+ sqle.getErrorCode()+" :");
            if (sqle.getErrorCode()==BOGARANTIA_NO_EXISTEIX){
                throw new BoGarantiaNoExisteixException(sqle.getMessage());
            }       
        }catch (Exception e){
            log.infoLog("altre excepcció");
        }
       
    }

    public void deleteBoGarantia(BoGarantia boGarantia) throws Exception {

        try{
            sqlMapClient.delete("bogarantia.delete",boGarantia);
        }catch (SQLException sqle){
            if (sqle.getErrorCode()==BOGARANTIA_NO_EXISTEIX){
                throw new BoGarantiaNoExisteixException(sqle.getMessage());
            }
            log.infoLog("delete bo SQLErrorCode: "+sqle.getErrorCode()+" :");
        }

       
    }

    public BoGarantia getBoGarantiaById(int idBoGarantia) throws Exception {

        BoGarantia bo=new BoGarantia();
        bo.setIdgarantia(idBoGarantia);

        try{
            bo=(BoGarantia)sqlMapClient.queryForObject("bogarantia.selectById", bo);
            if (bo==null){
                throw new BoGarantiaNoExisteixException();
            }
        }catch (SQLException sqle){
            if (sqle.getErrorCode() == BOGARANTIA_NO_EXISTEIX) {
                throw new BoGarantiaNoExisteixException(sqle.getMessage());
            }
        }

        return bo;


    }

    public List<BoGarantia> getBonsGarantiaByClient(Client client) throws Exception {
        List<BoGarantia> llista=new ArrayList<BoGarantia>();

       try{
        llista=(List<BoGarantia>)sqlMapClient.queryForList("bogarantia.getBoGarantiaLikeClient", client);
       }catch(SQLException sqle){
            if (sqle.getErrorCode() == BOGARANTIA_NO_EXISTEIX) {
                throw new BoGarantiaNoExisteixException(sqle.getMessage());
            }
       }

        return llista;
    }

    public List<BoGarantia> getBonsGarantiaByProducte(Producte producte) throws Exception {
        List<BoGarantia> llista=new ArrayList<BoGarantia>();

       try{
        llista=(List<BoGarantia>)sqlMapClient.queryForList("bogarantia.getBoGarantiaLikeProducte", producte);
       }catch(SQLException sqle){
            if (sqle.getErrorCode() == BOGARANTIA_NO_EXISTEIX) {
                throw new BoGarantiaNoExisteixException(sqle.getMessage());
            }
       }

        return llista;
    }
}
